/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package me.panpf.sketch.sample.widget.page;

import me.panpf.sketch.Sketch;
import me.panpf.sketch.request.DisplayOptions;
import me.panpf.sketch.request.ShapeSize;
import me.panpf.sketch.sample.AppConfig;
import me.panpf.sketch.sample.AssetImage;
import me.panpf.sketch.sample.ResourceTable;
import me.panpf.sketch.sample.widget.SampleImageView;
import me.panpf.sketch.shaper.RoundRectImageShaper;
import me.panpf.sketch.util.ExifInterface;
import me.panpf.sketch.util.Utils;
import ohos.agp.components.Component;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.LayoutScatter;
import ohos.agp.utils.Color;
import ohos.app.Context;
import ohos.media.image.PixelMap;
import ohos.system.DeviceInfo;

import static me.panpf.sketch.sample.AppConfig.Key.DISABLE_CORRECT_IMAGE_ORIENTATION;

/**
 * ImageOrientationTestView
 */
public class ImageOrientationTestView extends DirectionalLayout {

    private DisplayOptions displayOptions;
    private SampleImageView imageBefore;
    private SampleImageView imageAfter;

    public ImageOrientationTestView(Context context, int orientationExifTag, String filePath) {
        super(context, null);
        bindView(orientationExifTag, filePath);
    }

    private void bindView(int orientationExifTag, String filePath) {
        LayoutScatter layoutScatter = LayoutScatter.getInstance(getContext());
        Component view = layoutScatter.parse(ResourceTable.Layout_view_image_orientation_test, this, true);
        view.setWidth(Utils.getDisplayWidthInPx(getContext()));
        imageBefore = (SampleImageView) view.findComponentById(ResourceTable.Id_image_imageOrientationTest_before);
        imageAfter = (SampleImageView) view.findComponentById(ResourceTable.Id_image_imageOrientationTest_after);
        final int componentWidth = Utils.getDisplayWidthInPx(getContext())
                - imageAfter.getMarginLeft() * 2;
        PixelMap pixelMap = Utils.getPixelMap(getContext(), AssetImage.MEI_NV);
        Double aspectRatios = pixelMap.getImageInfo().size.width / (double) pixelMap.getImageInfo().size.height;
        DirectionalLayout.LayoutConfig layoutConfig = (DirectionalLayout.LayoutConfig) imageAfter.getLayoutConfig();
        RoundRectImageShaper roundRectImageShaper = new RoundRectImageShaper(0)
                .setContext(getContext());
        roundRectImageShaper.setStroke(Color.WHITE.getValue(), 0);
        boolean correctImageOrientationDisabled = AppConfig.getBoolean(getContext(), DISABLE_CORRECT_IMAGE_ORIENTATION);
        if (correctImageOrientationDisabled) {
            int componentHeight = (int) (componentWidth / aspectRatios);
            if (orientationExifTag == ExifInterface.ORIENTATION_ROTATE_90
                    || orientationExifTag == ExifInterface.ORIENTATION_ROTATE_270
                    || orientationExifTag == ExifInterface.ORIENTATION_TRANSPOSE
                    || orientationExifTag == ExifInterface.ORIENTATION_TRANSVERSE) {
                int distance = 162;
                if (DeviceInfo.getModel().startsWith("TAS")) {
                    distance = 168;
                } else if (DeviceInfo.getModel().startsWith("ANA")) {
                    distance = 162;
                }
                componentHeight = componentHeight + distance;
                roundRectImageShaper.setCorrectImageOrientationDisabled(true);
            } else if (orientationExifTag == ExifInterface.ORIENTATION_ROTATE_180
                    || orientationExifTag == ExifInterface.ORIENTATION_FLIP_HORIZONTAL
                    || orientationExifTag == ExifInterface.ORIENTATION_FLIP_VERTICAL) {
                layoutConfig.setMarginsTopAndBottom(80, 80);
            }
            pixelMap = Utils.getPixelMap(getContext(), filePath);
            aspectRatios = pixelMap.getImageInfo().size.width / (double) pixelMap.getImageInfo().size.height;
            displayOptions = new DisplayOptions().setShapeSize(new ShapeSize((int) (componentHeight * aspectRatios), componentHeight));
            layoutConfig.height = componentHeight;
            roundRectImageShaper.setRawPath(filePath);
            showImage(roundRectImageShaper, filePath, layoutConfig);
        } else {
            displayOptions = new DisplayOptions().setShapeSize(new ShapeSize(componentWidth, (int) (componentWidth / aspectRatios)));
            layoutConfig.height = (int) (componentWidth / aspectRatios);
            layoutConfig.setMarginsTopAndBottom(80, 80);
            roundRectImageShaper.setRawPath(AssetImage.MEI_NV);
            showImage(roundRectImageShaper, filePath, layoutConfig);
        }
    }

    private void showImage(RoundRectImageShaper roundRectImageShaper, String filePath, DirectionalLayout.LayoutConfig layoutConfig) {
        displayOptions.setShaper(roundRectImageShaper);
        imageAfter.setLayoutConfig(layoutConfig);
        imageAfter.setOptions(displayOptions);
        Sketch.with(getContext()).displayFromAsset(filePath, imageAfter).commit();
        imageBefore.setLayoutConfig(layoutConfig);
        imageBefore.setOptions(displayOptions);
        Sketch.with(getContext()).displayFromAsset(filePath, imageBefore).commit();
    }

}
